package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.InternalFrameEvent;

import model_layer.Product;
import ctr_layer.ProductCtr;

@SuppressWarnings("serial")
public class NewProductGUI extends JInternalFrame
{
	private Product product;
	private ProductCtr prod_ctr;
	private boolean edit;
	private JPanel contentPane;
	private JTextField id_tf;
	private JTextField name_tf;
	private JTextField quantity_tf;
	private JTextField min_amount_tf;
	private JTextField retail_price_tf;
	private JTextField profit_tf;
	private JTextField sale_price_tf;
	private JLabel[] inc_number_lbls;
	private JLabel inc_empty_name_lbl;
	private JLabel inc_name_lbl;
	private JLabel inc_taken_name_lbl;
	private JLabel inc_taken_id_lbl;
	private JButton btnCancel;
	private JLabel inc_name_number_lbl;

	/**
	 * Create the frame.
	 */
	public NewProductGUI(Product prod, boolean edit)
	{
		setIconifiable(true);
		setClosable(true);
		this.edit = edit;
		product = prod;
		prod_ctr = new ProductCtr();
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		try
		{
			setFrameIcon(new ImageIcon(NewProductGUI.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		setBounds(100, 100, 240, 300); 	//correct_size
		//setBounds(100, 100, 407, 300); //incorrect size
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		make_lbls();
		make_tfs();
		if(product == null)
		{
			setTitle("New Product");
			make_incorrect_lbls();
			make_new_btns();
			add_listeners();
		}
		else
		{
			fill_fields();
			if(edit)
			{
				setTitle("Edit Product");
				make_incorrect_lbls();
				make_edit_btns();
				add_listeners();
				id_tf.setEnabled(false);
			}
			else
			{
				setTitle("Product Details");
				make_display_btn();
				make_uneditable();
			}
		}
		
		
	}
	
	private void make_new_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnCreate = new JButton("Create");
		btnCreate.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCreate.setForeground(Color.WHITE);
		btnCreate.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCreate.setContentAreaFilled(false);
		btnCreate.setOpaque(true);
		btnCreate.setBorder(new RoundedCornerBorder());
		btnCreate.setBounds(10, 237, 89, 23);
		btnCreate.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				create_product();
				
			}
		});
		contentPane.add(btnCreate);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCancel.setForeground(Color.WHITE);
		btnCancel.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCancel.setContentAreaFilled(false);
		btnCancel.setOpaque(true);
		btnCancel.setBorder(new RoundedCornerBorder());
		btnCancel.setBounds(124, 237, 89, 23);
		btnCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				
			}
		});
		contentPane.add(btnCancel);
	}
	
	private void make_edit_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnEdit = new JButton("Save");
		btnEdit.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnEdit.setForeground(Color.WHITE);
		btnEdit.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnEdit.setContentAreaFilled(false);
		btnEdit.setOpaque(true);
		btnEdit.setBorder(new RoundedCornerBorder());
		btnEdit.setBounds(10, 237, 89, 23);
		btnEdit.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				edit_product();
				
			}
		});
		contentPane.add(btnEdit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCancel.setForeground(Color.WHITE);
		btnCancel.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCancel.setContentAreaFilled(false);
		btnCancel.setOpaque(true);
		btnCancel.setBorder(new RoundedCornerBorder());
		btnCancel.setBounds(124, 237, 89, 23);
		btnCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
			}
		});
		contentPane.add(btnCancel);
	}
	
	private void make_display_btn()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnOk = new JButton("OK");
		btnOk.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnOk.setForeground(Color.WHITE);
		btnOk.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnOk.setContentAreaFilled(false);
		btnOk.setOpaque(true);
		btnOk.setBorder(new RoundedCornerBorder());
		btnOk.setBounds(124, 237, 89, 23);
		btnOk.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				
			}
		});
		btnOk.setBounds(73, 236, 89, 23);
		contentPane.add(btnOk);
	}
	
	private void change_to_display_correct()
	{
		setSize(240, 300);
		btnCancel.setBounds(124, 237, 89, 23);
	}
	
	private void change_to_display_incorrect()
	{
		setSize(407, 300);
		btnCancel.setBounds(292, 237, 89, 23);
	}
	
	private void make_lbls()
	{
		JLabel lblId = new JLabel("ID");
		lblId.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblId.setForeground(Color.WHITE);
		lblId.setBounds(10, 11, 46, 14);
		contentPane.add(lblId);
		
		JLabel lblName = new JLabel("Name");
		lblName.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblName.setForeground(Color.WHITE);
		lblName.setBounds(10, 36, 46, 14);
		contentPane.add(lblName);
		
		JLabel lblQuantity = new JLabel("Quantity");
		lblQuantity.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblQuantity.setForeground(Color.WHITE);
		lblQuantity.setBounds(10, 61, 46, 14);
		contentPane.add(lblQuantity);
		
		JLabel lblMinimumAmount = new JLabel("Minimum amount");
		lblMinimumAmount.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblMinimumAmount.setForeground(Color.WHITE);
		lblMinimumAmount.setBounds(10, 86, 103, 14);
		contentPane.add(lblMinimumAmount);
		
		JLabel lblRetailPrice = new JLabel("Retail price");
		lblRetailPrice.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblRetailPrice.setForeground(Color.WHITE);
		lblRetailPrice.setBounds(10, 111, 66, 14);
		contentPane.add(lblRetailPrice);
		
		JLabel lblProfit = new JLabel("Profit");
		lblProfit.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblProfit.setForeground(Color.WHITE);
		lblProfit.setBounds(10, 136, 46, 14);
		contentPane.add(lblProfit);
		
		JLabel lblSalePrice = new JLabel("Sale price");
		lblSalePrice.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblSalePrice.setForeground(Color.WHITE);
		lblSalePrice.setBounds(10, 161, 66, 14);
		contentPane.add(lblSalePrice);

		JLabel label = new JLabel("%");
		label.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		label.setForeground(Color.WHITE);
		label.setBounds(202, 136, 11, 14);
		contentPane.add(label);
	}
	
	private void make_tfs()
	{
		id_tf = new JTextField();
		id_tf.setBounds(123, 8, 90, 20);
		id_tf.setBorder(new RoundedCornerBorder());
		id_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(id_tf);
		id_tf.setColumns(10);
		
		name_tf = new JTextField();
		name_tf.setBounds(123, 33, 90, 20);
		name_tf.setBorder(new RoundedCornerBorder());
		name_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(name_tf);
		name_tf.setColumns(10);
		
		quantity_tf = new JTextField();
		quantity_tf.setBounds(123, 58, 90, 20);
		quantity_tf.setBorder(new RoundedCornerBorder());
		quantity_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(quantity_tf);
		quantity_tf.setColumns(10);
		
		min_amount_tf = new JTextField();
		min_amount_tf.setBounds(123, 83, 90, 20);
		min_amount_tf.setBorder(new RoundedCornerBorder());
		min_amount_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(min_amount_tf);
		min_amount_tf.setColumns(10);
		
		retail_price_tf = new JTextField();
		retail_price_tf.setBounds(123, 108, 90, 20);
		retail_price_tf.setBorder(new RoundedCornerBorder());
		retail_price_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(retail_price_tf);
		retail_price_tf.setColumns(10);
		
		profit_tf = new JTextField();
		profit_tf.setBounds(123, 133, 73, 20);
		profit_tf.setBorder(new RoundedCornerBorder());
		profit_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(profit_tf);
		profit_tf.setColumns(10);
		
		sale_price_tf = new JTextField();
		sale_price_tf.setBounds(123, 158, 90, 20);
		sale_price_tf.setBorder(new RoundedCornerBorder());
		sale_price_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(sale_price_tf);
		sale_price_tf.setColumns(10);
		
	}
	
	private void add_listeners()
	{
		id_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					name_tf.requestFocus();
				}
			}
		});
		name_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					quantity_tf.requestFocus();
				}
			}
		});
		quantity_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					min_amount_tf.requestFocus();
				}
			}
		});
		min_amount_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					retail_price_tf.requestFocus();
				}
			}
		});
		retail_price_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					profit_tf.requestFocus();
				}
			}
		});
		profit_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					sale_price_tf.requestFocus();
				}
			}
		});
		profit_tf.addFocusListener(new FocusAdapter()
		{
			@Override
			public void focusLost(FocusEvent e)
			{
				calculate_and_set_sale_price();
			}
		});
		sale_price_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					calculate_and_set_profit();
					if(edit)
					{
						edit_product();
					}
					else
					{
						create_product();
					}
				}
			}
		});
		sale_price_tf.addFocusListener(new FocusAdapter()
		{
			@Override
			public void focusLost(FocusEvent e)
			{
				calculate_and_set_profit();
			}
		});
	}
	
	private void make_incorrect_lbls()
	{
		int[] y_positions = { 11, 61, 86, 111, 136, 161};
		inc_number_lbls = new JLabel[6];
		for (int i = 0; i < inc_number_lbls.length; i++)
		{
			JLabel temp = new JLabel("Must be a positive number.");
			temp.setForeground(Color.RED);
			temp.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
			temp.setBounds(224, y_positions[i], 170, 14);
			temp.setVisible(false);
			contentPane.add(temp);
			inc_number_lbls[i] = temp;
		}
		
		inc_name_lbl = new JLabel("Must be max 64 characters.");
		inc_name_lbl.setForeground(Color.RED);
		inc_name_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_name_lbl.setVisible(false);
		inc_name_lbl.setBounds(224, 36, 151, 14);
		contentPane.add(inc_name_lbl);
		
		inc_empty_name_lbl = new JLabel("Field cannot be empty.");
		inc_empty_name_lbl.setForeground(Color.RED);
		inc_empty_name_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_empty_name_lbl.setVisible(false);
		inc_empty_name_lbl.setBounds(224, 36, 131, 14);
		contentPane.add(inc_empty_name_lbl);
		
		inc_taken_name_lbl = new JLabel("Name is already taken");
		inc_taken_name_lbl.setForeground(Color.RED);
		inc_taken_name_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_taken_name_lbl.setVisible(false);
		inc_taken_name_lbl.setBounds(224, 36, 130, 14);
		contentPane.add(inc_taken_name_lbl);
		
		inc_taken_id_lbl = new JLabel("ID is already taken");
		inc_taken_id_lbl.setVisible(false);
		inc_taken_id_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_taken_id_lbl.setForeground(Color.RED);
		inc_taken_id_lbl.setBounds(224, 11, 114, 14);
		contentPane.add(inc_taken_id_lbl);
		
		inc_name_number_lbl = new JLabel("Cannot be a number.");
		inc_name_number_lbl.setForeground(Color.RED);
		inc_name_number_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_name_number_lbl.setBounds(224, 36, 130, 14);
		inc_name_number_lbl.setVisible(false);
		contentPane.add(inc_name_number_lbl);
	}
	
	private void fill_fields()
	{
		id_tf.setText(product.getId());
		name_tf.setText(product.getName());
		quantity_tf.setText(String.valueOf(product.getQuantity()));
		min_amount_tf.setText(String.valueOf(product.getMin_amount()));
		float retail_price = product.getLast_retail_price();
		if(retail_price == 0)
		{
			retail_price_tf.setText("");
		}
		else
		{
			retail_price_tf.setText(String.valueOf(retail_price));
		}
		sale_price_tf.setText(String.valueOf(product.getPrice()));
		calculate_and_set_profit();
	}
	
	private void make_uneditable()
	{
		id_tf.setEditable(false);
		name_tf.setEditable(false);
		quantity_tf.setEditable(false);
		min_amount_tf.setEditable(false);
		retail_price_tf.setEditable(false);
		profit_tf.setEditable(false);
		sale_price_tf.setEditable(false);
	}

	private void create_product()
	{
		if(check_for_correctnes())
		{
			String id = id_tf.getText();
			String name = name_tf.getText();
			String quantity = quantity_tf.getText();
			String min_amount = min_amount_tf.getText();
			String ret_price = retail_price_tf.getText();
			String sale_price = sale_price_tf.getText();
			if (ret_price.length() == 0)
			{
				ret_price = "0";
			}
			
			if(min_amount.length() == 0)
			{
				min_amount = "0";
			}
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to create this product?", "Save", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				int rc = prod_ctr.insert_product(id, name, Integer.parseInt(quantity), Integer.parseInt(min_amount),
						Float.parseFloat(ret_price),Float.parseFloat(sale_price));
				if(rc == 1)
				{
					JOptionPane.showMessageDialog(this, "Product was successfully created!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else if(rc == -2)
				{
					JOptionPane.showMessageDialog(this, "Desired id or name is taken, please choose a new one!\nProduct was not created.", "Error", JOptionPane.ERROR_MESSAGE);
					check_for_correctnes();
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while inserting a product!\nProduct was not created.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private void edit_product()
	{
		if(check_for_correctnes())
		{
			String name = name_tf.getText();
			String quantity = quantity_tf.getText();
			String min_amount = min_amount_tf.getText();
			String ret_price = retail_price_tf.getText();
			if (ret_price.length() == 0)
			{
				ret_price = "0";
			}
			
			if(min_amount.length() == 0)
			{
				min_amount = "0";
			}
			String sale_price = sale_price_tf.getText();
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to update this product?", "Save", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				product.setLast_retail_price(Float.parseFloat(ret_price));
				product.setName(name);
				product.setMin_amount(Integer.parseInt(min_amount));
				product.setPrice(Float.parseFloat(sale_price));
				product.setQuantity(Integer.parseInt(quantity));
				int rc = prod_ctr.update_product(product);
				if(rc == 1)
				{
					JOptionPane.showMessageDialog(this, "Product was successfully updated!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else if (rc == -2)
				{
					JOptionPane.showMessageDialog(this, "Desired name is taken, please choose a new one!\nProduct was not updated.", "Error", JOptionPane.ERROR_MESSAGE);
					check_for_correctnes();
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while saving a product!\nProduct was not updated.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private boolean check_for_correctnes()
	{
		boolean correct = true;
		String id = id_tf.getText();
		String name = name_tf.getText();
		String quantity = quantity_tf.getText();
		String min_amount = min_amount_tf.getText();
		String ret_price = retail_price_tf.getText();
		String profit = profit_tf.getText();
		String sale_price = sale_price_tf.getText();
		if(!edit)
		{
			if(!is_correct_int_value(id))
			{
				inc_number_lbls[0].setVisible(true);
				inc_taken_id_lbl.setVisible(false);
				correct = false;
			}
			else if(prod_ctr.id_exists(id))
			{
				inc_number_lbls[0].setVisible(false);
				inc_taken_id_lbl.setVisible(true);
				correct = false;
			}
			else
			{
				inc_number_lbls[0].setVisible(false);
				inc_taken_id_lbl.setVisible(false);
			}
		}
		
		if(name.length() > 64)
		{
			inc_name_number_lbl.setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			inc_name_lbl.setVisible(true);
			inc_empty_name_lbl.setVisible(false);
			correct = false;
		}
		else if(name.length() == 0)
		{
			inc_name_number_lbl.setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			inc_name_lbl.setVisible(false);
			inc_empty_name_lbl.setVisible(true);
			correct = false;
		}
		else if(is_int(name))
		{
			inc_name_number_lbl.setVisible(true);
			inc_taken_name_lbl.setVisible(false);
			inc_name_lbl.setVisible(false);
			inc_empty_name_lbl.setVisible(false);
			correct = false;
		}
		else if((edit && !product.getName().equals(name) && prod_ctr.name_exists(name)) || (!edit && prod_ctr.name_exists(name)))
		{
			inc_name_number_lbl.setVisible(false);
			inc_taken_name_lbl.setVisible(true);
			inc_name_lbl.setVisible(false);
			inc_empty_name_lbl.setVisible(false);
			correct = false;
		}
		else
		{
			inc_name_number_lbl.setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			inc_name_lbl.setVisible(false);
			inc_empty_name_lbl.setVisible(false);
		}
		
		
		if(!is_correct_int_value(quantity))
		{
			inc_number_lbls[1].setVisible(true);
			correct = false;
		}
		else inc_number_lbls[1].setVisible(false);
		
		if(min_amount.length() != 0 && !is_correct_int_value(min_amount))
		{
			inc_number_lbls[2].setVisible(true);
			correct = false;
		}
		else inc_number_lbls[2].setVisible(false);
		
		if(ret_price.length() != 0 && !is_correct_float_value(ret_price))
		{
			inc_number_lbls[3].setVisible(true);
			correct = false;
		}
		else inc_number_lbls[3].setVisible(false);
		
		if(profit.length() != 0 && !is_correct_float_value(profit))
		{
			inc_number_lbls[4].setVisible(true);
			correct = false;
		}
		else inc_number_lbls[4].setVisible(false);
		
		if(!is_correct_float_value(sale_price))
		{
			inc_number_lbls[5].setVisible(true);
			correct = false;
		}
		else inc_number_lbls[5].setVisible(false);
		
		if(!correct)
		{
			change_to_display_incorrect();
		}
		else
		{
			change_to_display_correct();
		}
		
		return correct;
	}
	
	private boolean is_correct_int_value(String input)
	{
		try
		{
			int i = Integer.parseInt(input);
			return i >= 0;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	private boolean is_correct_float_value(String input)
	{
		try
		{
			float f = Float.parseFloat(input);
			return f > 0;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	private boolean is_int(String input)
	{
		try
		{
			Integer.parseInt(input);
			return true;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	private void calculate_and_set_profit()
	{
		String ret_price = retail_price_tf.getText();
		String sale_price = sale_price_tf.getText();
		if(is_correct_float_value(ret_price) && is_correct_float_value(sale_price))
		{
			float s_price = Float.parseFloat(sale_price);
			float r_price = Float.parseFloat(ret_price);
			float profit = ((s_price - r_price)/r_price) * 100;
			profit_tf.setText(String.valueOf(profit));
		}
		else if(!is_correct_float_value(profit_tf.getText()))
		{
			profit_tf.setText("");
		}
	}
	
	private void calculate_and_set_sale_price()
	{
		String ret_price = retail_price_tf.getText();
		String profit = profit_tf.getText();
		if(is_correct_float_value(ret_price) && is_correct_float_value(profit))
		{
			float prof = Float.parseFloat(profit);
			float r_price = Float.parseFloat(ret_price);
			float sale_price = prof * r_price / 100 + r_price;
			sale_price_tf.setText(String.valueOf(sale_price));
		}
		else if(!is_correct_float_value(sale_price_tf.getText()))
		{
			sale_price_tf.setText("");
		}
	}
}
